package smsapi.api;


import org.springframework.http.*;
import org.springframework.stereotype.Component;
import smsapi.*;
import smsapi.model.Token;

@Component
public class UserApi extends ApiBase {

    final String localVarPath = "/api/User/";
    /**
     * 获取令牌
     *
     * @param timestamp 时间戳 (required)
     * @param nonce     仅仅被使用一次 (required)
     * @return Token
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public Token getToken(String timestamp, String nonce) throws ApiException {
        getTokenValidateBeforeCall(companyNo, timestamp, nonce);
        final StringBuilder url = new StringBuilder();
        url.append(basepath).append(localVarPath).append(escapeString(companyNo));
        HttpHeaders headers = new HttpHeaders();
        headers.add("timestamp", timestamp);
        headers.add("nonce", nonce);
        headers.add("companyNo", companyNo);
        headers.add("loginName", loginName);
        String signature = generateSignature(timestamp, nonce);
        headers.add("signature", signature);

        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        HttpEntity<?> entity = new HttpEntity<>(null, headers);
        ResponseEntity<Token> resultResponseEntity = restTemplate.exchange(url.toString(),HttpMethod.GET, entity, Token.class);
        if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
            return resultResponseEntity.getBody();
        }else {
            throw new ApiException(resultResponseEntity.getStatusCodeValue(),resultResponseEntity.getStatusCode().getReasonPhrase(),null,resultResponseEntity.getBody().toString());
        }
    }

    private void getTokenValidateBeforeCall(String id, String timestamp, String nonce) throws ApiException {
        // verify the required parameter 'id' is set
        if (id == null) {
            throw new ApiException("Missing the required parameter 'id' when calling getToken");
        }

        // verify the required parameter 'timestamp' is set
        if (timestamp == null) {
            throw new ApiException("Missing the required parameter 'timestamp' when calling getToken");
        }

        // verify the required parameter 'nonce' is set
        if (nonce == null) {
            throw new ApiException("Missing the required parameter 'nonce' when calling getToken");
        }

        // verify the required parameter 'companyNo' is set
        if (companyNo == null) {
            throw new ApiException("Missing the required parameter 'companyNo' when calling getToken");
        }

        // verify the required parameter 'loginName' is set
        if (loginName == null) {
            throw new ApiException("Missing the required parameter 'loginName' when calling getToken");
        }
    }
}
